package com.cls.business.controller;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.cls.common.annotation.ControllerEndpoint;
import com.cls.common.utils.MyUtil;
import com.cls.common.entity.MyConstant;
import com.cls.common.controller.BaseController;
import com.cls.common.entity.QueryRequest;
import com.cls.common.entity.ResponseVO;
import com.github.pagehelper.PageInfo;
import com.cls.business.entity.ReviewDetail;
import com.cls.business.service.IReviewDetailService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PathVariable;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 专家评审结果详细表 Controller
 *
 * @author wandering
 * @date 2020-11-18 11:59:01
 */
@Slf4j
@Validated
@Controller
@RequiredArgsConstructor
public class ReviewDetailController extends BaseController {

    private final IReviewDetailService reviewDetailService;

    /**
    * 专家评审结果详细表列表页面
    **/
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/reviewDetail")
    public String reviewDetailIndex(){
        return MyUtil.view("business/reviewDetail/reviewDetail");
    }

    /**
    * 专家评审结果详细表新增页面
    * @return
    */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/reviewDetail/reviewDetailAdd")
    public String reviewDetailAdd(Model model){
        return MyUtil.view("business/reviewDetail/reviewDetailAdd");
    }

    /**
    * 专家评审结果详细表修改页面
    * @return
    */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/reviewDetail/reviewDetailUpdate/{reviewDetailId}")
    public String reviewDetailUpdate(@PathVariable Long reviewDetailId, Model model){
        ReviewDetail reviewDetail = reviewDetailService.getById(reviewDetailId);
        model.addAttribute("reviewDetail",reviewDetail);
        return MyUtil.view("business/reviewDetail/reviewDetailUpdate");
    }



    /**
    * 专家评审结果详细表详情页面
    * @return
    */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/reviewDetail/reviewDetailDetail/{reviewDetailId}")
    public String reviewDetailDetail(@PathVariable Long reviewDetailId, Model model){
        ReviewDetail reviewDetail = reviewDetailService.getById(reviewDetailId);
        model.addAttribute("reviewDetail",reviewDetail);
        return MyUtil.view("business/reviewDetail/reviewDetailDetail");
    }

    @GetMapping("reviewDetail")
    @ResponseBody
    @RequiresPermissions("reviewDetail:list")
    public ResponseVO getAllReviewDetails(ReviewDetail reviewDetail) {
        return ResponseVO.success(reviewDetailService.findReviewDetails(reviewDetail));
    }

    @GetMapping("reviewDetail/list")
    @ResponseBody
    @RequiresPermissions("reviewDetail:list")
    public ResponseVO reviewDetailList(QueryRequest request, ReviewDetail reviewDetail) {
        PageInfo<ReviewDetail> pageInfo =  this.reviewDetailService.findReviewDetails(request, reviewDetail);
        return ResponseVO.success(pageInfo);
    }

    @ControllerEndpoint(operation = "新增ReviewDetail", exceptionMessage = "新增ReviewDetail失败")
    @PostMapping("reviewDetail")
    @ResponseBody
    @RequiresPermissions("reviewDetail:add")
    public ResponseVO addReviewDetail(@Valid ReviewDetail reviewDetail) {
        this.reviewDetailService.createReviewDetail(reviewDetail);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "多个删除ReviewDetail", exceptionMessage = "删除ReviewDetail失败")
    @PostMapping("reviewDetail/deletes/{ids}")
    @ResponseBody
    @RequiresPermissions("reviewDetail:delete")
    public ResponseVO deleteReviewDetails(@PathVariable String ids) {
        String[] idArry = ids.split(StringPool.COMMA);
        this.reviewDetailService.deleteReviewDetails(idArry);
        return ResponseVO.success();
    }


    @ControllerEndpoint(operation = "删除单个ReviewDetail", exceptionMessage = "删除ReviewDetail失败")
    @PostMapping("reviewDetail/delete/{reviewDetailId}")
    @ResponseBody
    @RequiresPermissions("reviewDetail:delete")
    public ResponseVO deleteReviewDetail(@PathVariable Long reviewDetailId) {
        this.reviewDetailService.removeById(reviewDetailId);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "修改ReviewDetail", exceptionMessage = "修改ReviewDetail失败")
    @PostMapping("reviewDetail/update")
    @ResponseBody
    @RequiresPermissions("reviewDetail:update")
    public ResponseVO updateReviewDetail(ReviewDetail reviewDetail) {
        this.reviewDetailService.updateReviewDetail(reviewDetail);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "修改ReviewDetail", exceptionMessage = "导出Excel失败")
    @PostMapping("reviewDetail/excel")
    @ResponseBody
    @RequiresPermissions("reviewDetail:export")
    public void export(QueryRequest queryRequest, ReviewDetail reviewDetail, HttpServletResponse response) {
        List<ReviewDetail> reviewDetails = this.reviewDetailService.findReviewDetails(queryRequest, reviewDetail).getList();
        ExcelKit.$Export(ReviewDetail.class, response).downXlsx(reviewDetails, false);
    }
}
